package speed.mvc.servlet;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

/**
 * @author zhaohaolin(zhaohaolin2010@gmail.com)
 */
public class RequestUtils {

	public static String getString(HttpServletRequest request, String _name) {
		return request.getParameter(_name);
	}

	public static int getInt(HttpServletRequest request, String name,
			int _default) {
		String value = request.getParameter(name);
		return StringUtils.isEmpty(value) == true ? _default : Integer
				.parseInt(value);
	}

	public static double getDouble(HttpServletRequest request, String name,
			double _default) {
		String value = request.getParameter(name);
		return StringUtils.isEmpty(value) == true ? _default : Double
				.parseDouble(value);
	}

	public static long getLong(HttpServletRequest request, String name,
			long _default) {
		String value = request.getParameter(name);
		return StringUtils.isEmpty(value) == true ? _default : Long
				.parseLong(value);
	}

	public static float getFloat(HttpServletRequest request, String name,
			float _default) {
		String value = request.getParameter(name);
		return StringUtils.isEmpty(value) == true ? _default : Float
				.parseFloat(value);
	}

	public static boolean getBoolean(HttpServletRequest request, String name,
			boolean _default) {
		String value = request.getParameter(name);
		return StringUtils.isEmpty(value) == true ? _default : Boolean
				.parseBoolean(value);
	}

	@SuppressWarnings("deprecation")
	public static Date getDate(HttpServletRequest request, String name,
			Date _default) {
		String value = request.getParameter(name);
		return StringUtils.isEmpty(value) == true ? _default : new Date(value);
	}

}
